package com.xinrui.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinrui.sunnyday.dao.CheckGroupDao;
import com.xinrui.sunnyday.dao.CheckItemDao;
import com.xinrui.sunnyday.entity.QueryPageBean;
import com.xinrui.sunnyday.exception.SunnydayException;
import com.xinrui.sunnyday.pojo.CheckGroup;
import com.xinrui.sunnyday.pojo.CheckItem;
import com.xinrui.sunnyday.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

@Transactional
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageInfo<CheckGroup> findPage(QueryPageBean queryPageBean) {
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())) {
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<CheckGroup> list = checkGroupDao.findPage(queryPageBean.getQueryString());
        return new PageInfo<>(list);
    }

    /**
     * 添加
     * @param checkGroup
     * @param checkItemIds
     * @throws SunnydayException
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        if (checkItemIds.length <= 0) {
            throw new SunnydayException("所选检查项不能为空！");
        }
        checkGroupDao.groupAdd(checkGroup);
        HashMap<String, Object> map = new HashMap<>();
        map.put("groupId", checkGroup.getId());
        map.put("itemIds", checkItemIds);
        checkGroupDao.itemAndGroupAdd(map);
    }

    /**
     * 查询检查组数据
     * @param id
     * @return
     */
    @Override
    public CheckGroup findGroupById(Integer id) {
        return checkGroupDao.findGroupById(id);
    }

    /**
     * 查询检查组和检查项的中间表中检查组ids
     * @param id
     * @return
     */
    @Override
    public List<Integer> findGroupAndItemById(Integer id) {
        return checkGroupDao.findGroupAndItemById(id);
    }

    /**
     * 编辑
     * @param checkGroup
     * @param checkItemIds
     * @throws SunnydayException
     */
    @Override
    public void update(CheckGroup checkGroup, Integer[] checkItemIds) throws SunnydayException {
        if (checkItemIds.length <= 0) {
            throw new SunnydayException("所选检查项不能为空！");
        }
        checkGroupDao.update(checkGroup);
        checkGroupDao.deleteGroupAndItemById(checkGroup.getId());
        HashMap<String, Object> map = new HashMap<>();
        map.put("groupId", checkGroup.getId());
        map.put("itemIds", checkItemIds);
        checkGroupDao.itemAndGroupAdd(map);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) throws SunnydayException{
        int count = checkGroupDao.findSetmealByGroupId(id);
        if (count > 0){
            throw new SunnydayException("该检查组已被套餐所关联");
        }
        checkGroupDao.deleteGroupAndItemById(id);
        checkGroupDao.deleteById(id);
    }
}
